package study2018.com.xtianzhuang.www.thread;

/**
 * @ClassName: ThreadlocalTest
 * @author : xtian.zhuang
 * @date 2018年7月16日 上午9:39:13
 *
 */
public class ThreadlocalTest {
	private static final ThreadLocal<Message> MESSAGE_LOCAL = new ThreadLocal<Message>();

	public static void setMessage(Message message) {
		MESSAGE_LOCAL.set(message);
	}

	public static void send() {
		System.out.println(Thread.currentThread().getName() + "[消息发送]" + MESSAGE_LOCAL.get().getInfo());
	}


	public static void main(String[] args) {
		new Thread(() -> {
			Message msg = new Message();
			msg.setInfo("first");
			ThreadlocalTest.setMessage(msg);
			ThreadlocalTest.send();

		}, "first").start();
		new Thread(() -> {
			Message msg = new Message();
			msg.setInfo("second");
			ThreadlocalTest.setMessage(msg);
			ThreadlocalTest.send();

		}, "second").start();
		new Thread(() -> {
			Message msg = new Message();
			msg.setInfo("third");
			ThreadlocalTest.setMessage(msg);
			ThreadlocalTest.send();

		}, "third").start();
	}


}

class Message {
	private String info;

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

}